package com.atguigu.webapp.controller.agreement;


import com.atguigu.common.constant.LoginUserContext;
import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.LeaseAgreement;
import com.atguigu.model.enums.LeaseStatus;
import com.atguigu.webapp.service.LeaseAgreementService;
import com.atguigu.webapp.vo.agreement.AgreementDetailVo;
import com.atguigu.webapp.vo.agreement.AgreementItemVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/agreement")
@Tag(name = "租约信息")
@CrossOrigin
public class LeaseAgreementController {

    @Autowired
    private LeaseAgreementService leaseAgreementService;


    @Operation(summary = "获取个人租约基本信息列表")
    @GetMapping("listItem")
    public Result<List<AgreementItemVo>> listAgreementItemByPhone() {
        List<AgreementItemVo> agreementItemVos = leaseAgreementService.listAgreementItemByPhone(LoginUserContext.getLoginUser().getUsername());
        return Result.ok(agreementItemVos);
    }

    @Operation(summary = "根据id获取租约详细信息")
    @Parameters({
            @Parameter(name = "id",description = "租约id",in = ParameterIn.QUERY)
    })
    @GetMapping("getDetailById")
    public Result<AgreementDetailVo> getAgreementDetailById(@RequestParam Long id) {
        AgreementDetailVo detailVo = leaseAgreementService.getAgreementDetailById(id);
        return Result.ok(detailVo);
    }

    @Operation(summary = "根据id更新租约状态", description = "用于确认租约和提前退租")
    @Parameters({
            @Parameter(name = "id",description = "租约id",in = ParameterIn.QUERY),
            @Parameter(name = "leaseStatus",description = "租约状态",in = ParameterIn.DEFAULT)
    })
    @PostMapping("updateStatusById")
    public Result<Boolean> updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus leaseStatus) {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LeaseAgreement::getId,id);
        updateWrapper.set(LeaseAgreement::getStatus,leaseStatus);
        boolean update = leaseAgreementService.update(updateWrapper);
        return Result.ok(update);
    }

    @Operation(summary = "保存或更新租约", description = "用于续约")
    @Parameters({
            @Parameter(name = "leaseAgreement",description = "租约对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        boolean b = leaseAgreementService.saveOrUpdate(leaseAgreement);
        return Result.ok(b);
    }
}